package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 菜品管理
 * @author Xiao
 * @date 2021/9/26
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        log.info("dishDto:{}",dishDto.getName());
        dishService.addDish(dishDto);
        return R.success("添加成功！");
    }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> getDishPage(@RequestParam Integer page, Integer pageSize, String name) {
        log.info("page:{}, pageSize:{}, name:{}", page, pageSize, name);
        Page<DishDto> dishPageDto = dishService.getDishPage(page, pageSize, name);
        return R.success(dishPageDto);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable Long id) {
        log.info("id:{}",id);
        DishDto dishDto = dishService.queryDishById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> editDish(@RequestBody DishDto dishDto) {
        log.info("dishDto:{}", dishDto.toString());
        dishService.editDish(dishDto);
        return R.success("修改成功！");
    }

    /**
     * (批量)修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> dishStatusByStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("status:{}, ids:{}",status, ids);
        dishService.dishStatusByStatus(status, ids);
        return R.success("修改成功！");
    }

    /**
     * (批量)删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteDish(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);
        boolean result = dishService.deleteDish(ids);
        if (result) {
            return R.success("删除成功！");
        }
        return R.error("删除失败，在售状态无法删除！");
    }

    /**
     * 根据分类id或菜品name查询
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> queryDishList(Dish dish) {
        log.info("dish:{}", dish.toString());
        List<DishDto> dishDtos = dishService.queryDishList(dish);
        return R.success(dishDtos);
    }
}
